package cn.vehicle.biz.eventListener;

import cn.vehicle.biz.event.PlateDetectedResultUploadGovEvent;
import cn.vehicle.constants.Constants;
import cn.vehicle.domains.dto.BaseResponse;
import cn.vehicle.domains.entity.CommonVehicleEntity;
import cn.vehicle.domains.entity.TransportVehicleEntity;
import cn.vehicle.domains.entity.VehicleCommonLedgerEntity;
import cn.vehicle.domains.entity.VehicleTransportLedgerEntity;
import cn.vehicle.enums.GovUploadStatus;
import cn.vehicle.enums.InOutTypeEnum;
import cn.vehicle.service.CommonVehicleService;
import cn.vehicle.service.TransportVehicleService;
import cn.vehicle.service.VehicleCommonLedgerService;
import cn.vehicle.service.VehicleTransportLedgerService;
import cn.vehicle.service.remote.TjRemoteService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.Semaphore;

@Component
@Slf4j
public class PlateDetectedResultUploadGovListener implements ApplicationListener<PlateDetectedResultUploadGovEvent> {

    @Resource
    private VehicleTransportLedgerService vehicleTransportLedgerService;

    @Resource
    private TjRemoteService tjRemoteService;

    @Resource
    private VehicleCommonLedgerService vehicleCommonLedgerService;

    @Resource
    private CommonVehicleService commonVehicleService;

    @Resource
    private TransportVehicleService transportVehicleService;

    @Override
    public void onApplicationEvent(PlateDetectedResultUploadGovEvent event) {
        Long id = event.getId();

        VehicleTransportLedgerEntity vehicleTransportLedgerEntity = vehicleTransportLedgerService.getTransportLedgerById(id);

        if (vehicleTransportLedgerEntity == null) {
            log.error("未找到车辆台账信息");
            return;
        }

        VehicleCommonLedgerEntity vehicleCommonLedgerEntity = vehicleCommonLedgerService.getVehicleCommonLedgerByAssId(id);

        CommonVehicleEntity commonVehicleEntity = commonVehicleService.getCommonVehicleByVehicleNumber(vehicleCommonLedgerEntity.getVehicleNumber());

        TransportVehicleEntity transportVehicleEntity = transportVehicleService.getTransportVehicleById(commonVehicleEntity.getAssociationId());


        // 创建更新对象，用于记录政府上报状态
        VehicleTransportLedgerEntity updateEntity = new VehicleTransportLedgerEntity();
        updateEntity.setId(transportVehicleEntity.getId());
        try {
            // 调用天津远程服务上报车辆通行信息
            BaseResponse baseResponse = tjRemoteService.putVehiclePassRecords(vehicleTransportLedgerEntity, event.getInOutType(), vehicleCommonLedgerEntity, commonVehicleEntity, transportVehicleEntity);
            // 若返回结果为成功，设置状态为 SUCCESS，否则标记为 FAIL
            if (baseResponse != null && baseResponse.getCode().equals(Constants.GOV_SUCCESS_CODE)) {
                setUploadStatus(updateEntity, event.getInOutType(), GovUploadStatus.SUCCESS);
            } else {
                setUploadStatus(updateEntity, event.getInOutType(), GovUploadStatus.FAIL);
            }

            // 捕获网络或加密传输异常，并记录日志
        } catch (Exception e) {
            log.error("车辆信息上班政府异常", e);
            setUploadStatus(updateEntity, event.getInOutType(), GovUploadStatus.FAIL);
        }

        // 更新运输车辆的政府上报状态到数据库
        vehicleTransportLedgerService.updateTransportExitInfo(updateEntity);
    }


    public void setUploadStatus(VehicleTransportLedgerEntity updateEntity, InOutTypeEnum inOutType, GovUploadStatus status) {
        if (inOutType == InOutTypeEnum.IN) {
            updateEntity.setEntryUploadStatus(status.name());
        } else {
            updateEntity.setExitUploadStatus(status.name());
        }

        Semaphore semaphore = new Semaphore(1);
        boolean b = semaphore.tryAcquire();

        semaphore.release();

    }
}
